app.controller("buyCarCtr",["$scope","$stateParams","$http",function ($scope,$stateParams,$http) {
    $scope.obj=$stateParams.msg;
    $scope.count=0;
    $scope.add=function () {
        $scope.count=$scope.count*1+1;
    };
    $scope.jian=function () {

        $scope.count=$scope.count*1-1;

    };
    $scope.addmsg=function (data) {
        data.count=$scope.count;
        $http({
            method:"get",
            url:"http://localhost:6500/addmsg",
            params:data
        }).then(function (res) {
            console.log(res.data)
        })
    }
}]);
app.controller("buyCarCtr",["$scope","$http",function ($scope,$http) {
    //获取购物车中的所有数据
    $scope.isall=true;
    $scope.getAllMsg=function () {
        $http.get("http://localhost:6500/data/car.json").then(function (res) {
            $scope.arr=res.data;
            var arr=$scope.arr;
            var sum=0;
            for(var i=0;i<arr.length;i++){
                console.log(Boolean(arr[i].ischoose));
                if(Boolean(arr[i].ischoose)==true){
                    sum+=arr[i].count*arr[i].price;
                }else{
                    $scope.isall=false;
                }
            }
            $scope.sum=sum;
        })
    };
    //实现加按钮操作
    $scope.add=function (id) {
        $http({
            method:"get",
            url:"http://localhost:6500/changecount",
            params:{
                id:id,
                type:"+"
            }
        }).then(function (res) {
            $scope.getAllMsg();
        })
    };
    //实现减按钮操作
    $scope.jian=function (id) {
        $http({
            method:"get",
            url:"http://localhost:6500/changecount",
            params:{
                id:id,
                type:"-"
            }
        }).then(function (res) {
            $scope.getAllMsg();
        })
    };
    $scope.delmsg=function (id) {
        $http({
            method:"get",
            url:"http://localhost:6500/delmsg",
            params:{
                id:id
            }
        }).then(function (res) {
            console.log(res.data);
            $scope.getAllMsg();
        })
    };
    $scope.getSum=function(){
        $scope.all=true;
        $scope.sum=0;
        for (var i=0;i<$scope.arr.length;i++) {
            if ($scope.arr[i].ischoose) {
                $scope.sum=$scope.sum+$scope.arr[i].price*$scope.arr[i].count;
            }else{
                $scope.all=false;
            }
        }
    };
    $scope.allchoose=function(){
        $scope.all = !$scope.all;
        //前台页面的修改
        for (var i=0;i<$scope.arr.length;i++) {
            $scope.arr[i].ischoose=$scope.all;
        }
        $http({
            method:"get",
            url:"http://localhost:6500/changecarmsg",
            params:{
                ischoose:$scope.all
            }
        }).then(function(res){
            $scope.getSum()
        })
    };
    $scope.onechoose = function(id,choose){
        for (var i = 0; i < $scope.arr.length ; i++) {
            if (id == $scope.arr[i].id) {
                $scope.arr[i].ischoose = !choose;
            }
        }
        $http({
            method:"get",
            url:"http://localhost:6500/ischoosecarmsg",
            params:{
                id:id,
                ischoose:!choose
            }
        }).then(function(res){
            $scope.getSum()
        })
    };
    $scope.getAllMsg();
}]);

